package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.MbrMemberPraiseMapper;
import com.macro.mall.model.MbrMemberPraise;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.MbrMemberPraiseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * MbrMemberPraise的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class MbrMemberPraiseServiceImpl implements MbrMemberPraiseService {

    private boolean isDebugEnabled;

	@Autowired
	private MbrMemberPraiseMapper mbrMemberPraiseMapper;

	@Override
	public List<MbrMemberPraise> find(MbrMemberPraise value) {
		//TODO这里可以做通过Assist做添加查询
		List<MbrMemberPraise> result = mbrMemberPraiseMapper.selectMbrMemberPraise(null);
		if (isDebugEnabled) {
			log.debug("执行获取MbrMemberPraise数据集-->结果:", result);
		}
		return result;
	}
	@Override
	public MbrMemberPraise findOne(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过MbrMemberPraise的id获得MbrMemberPraise对象-->失败:id不能为空");
			}
            Asserts.fail("执行通过MbrMemberPraise的id获得MbrMemberPraise对象-->失败:id不能为空");
		}
		MbrMemberPraise result = mbrMemberPraiseMapper.selectMbrMemberPraiseById(id);
		if (isDebugEnabled) {
			log.debug("执行通过MbrMemberPraise的id获得MbrMemberPraise对象-->结果:", result);
		}
		return result;
	}

	@Override
	public CommonResult saveNotNull(MbrMemberPraise value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将MbrMemberPraise中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
		int result = mbrMemberPraiseMapper.insertNotNullMbrMemberPraise(value);
		if (isDebugEnabled) {
			log.debug("执行将MbrMemberPraise中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}
	@Override
	public CommonResult updateNotNullById(MbrMemberPraise value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行通过MbrMemberPraise的id更新MbrMemberPraise中属性不为null的数据-->失败:对象为null");
			}
                Asserts.fail("执行通过MbrMemberPraise的id更新MbrMemberPraise中属性不为null的数据-->失败:对象为null");
		}
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
            log.error("",e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        value.setMemberId(userId);
		int result = mbrMemberPraiseMapper.updateNotNullMbrMemberPraiseById(value);
		if (isDebugEnabled) {
			log.debug("执行通过MbrMemberPraise的id更新MbrMemberPraise中属性不为null的数据-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
	public CommonResult deleteById(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过MbrMemberPraise的id删除MbrMemberPraise-->失败:id不能为空");
			}
                Asserts.fail("执行通过MbrMemberPraise的id删除MbrMemberPraise-->失败:id不能为空");
		}
		int result = mbrMemberPraiseMapper.deleteMbrMemberPraiseById(id);
		if (isDebugEnabled) {
			log.debug("执行通过MbrMemberPraise的id删除MbrMemberPraise-->结果:", result);
		}
        return CommonResult.success(null);
	}

    @Override
    public void deleteByPraise(MbrMemberPraise mbrMemberPraise) {
        if (mbrMemberPraise == null) {
            if (isDebugEnabled) {
                log.debug("执行将mbrMemberPraise中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
            log.error("",e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        Assist assist = new Assist();
        assist.andIn("member_id", userId);
        if (StrUtil.isEmptyIfStr(mbrMemberPraise.getTargetType())) {
            Asserts.fail("失败:TargetType不能为空");
        }
            assist.andIn("target_type", mbrMemberPraise.getTargetType());
        if (StrUtil.isEmptyIfStr(mbrMemberPraise.getTargetId())) {
            Asserts.fail("失败:TargetType不能为空");
        }
        assist.andIn("target_id", mbrMemberPraise.getTargetId());
        int result = mbrMemberPraiseMapper.deleteMbrMemberPraiseByAssist(assist);
        if (isDebugEnabled) {
            log.debug("执行将MbrMemberVideo中属性值不为null的数据保存到数据库-->结果:", result);
        }
    }


}